package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.entity.Order;
import com.skyc.service.AdminOrderService;
import com.skyc.service.impl.AdminOrderServiceImpl;

@WebServlet("/AdminsellerorderServlet")
public class AdminsellerorderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		ServletContext application = getServletContext();
		String op = request.getParameter("op");

		AdminOrderService aos = new AdminOrderServiceImpl();

		if (op.equals("del")) {
			int id = Integer.valueOf(request.getParameter("id"));
			aos.deleteByOID(id);
			response.sendRedirect("AdminsellerorderServlet?op=page&page=1");
		}

		else if (op.equals("updatefahuo")) {
			Order o = new Order();
			int id = Integer.valueOf(request.getParameter("id"));
			o.setOid(id);
			o.setOsend(1);
			aos.updateByOsend(o);
			response.sendRedirect("AdminsellerorderServlet?op=page&page=1");
		} else if (op.equals("updateshouhuo")) {
			Order o = new Order();
			int id = Integer.valueOf(request.getParameter("id"));
			o.setOid(id);
			o.setOstate(1);
			aos.updateByOstate(o);
			response.sendRedirect("AdminsellerorderServlet?op=page&page=1");
		} else if (op.equals("updatetuihuo")) {
			Order o = new Order();
			int id = Integer.valueOf(request.getParameter("id"));
			o.setOid(id);
			o.setOtuihuo(3);
			aos.updateByOtuihuo(o);
			response.sendRedirect("AdminsellerorderServlet?op=page&page=1");
		} else if (op.equals("updateorder")) {
			Order o = new Order();
			int id = Integer.valueOf(request.getParameter("id"));
			o.setOid(id);
			o.setOorder(1);
			aos.updateByOorder(o);
			response.sendRedirect("AdminsellerorderServlet?op=page&page=1");
		} else if (op.equals("query")) {
			// 用户列表查询
		
		} else if (op.equals("page")) {
			int counts = 0;
			int countState = 0;
			int countPayed = 0;
			int countSend = 0;
			int countStates = 0;
			int countTuihuo = 0;
			int countTuihuochuli = 0;
			List<Order> orderList1 = aos.query();
			List<Order> orderListState = new ArrayList<Order>();// 待收货
			List<Order> orderListPayed = new ArrayList<Order>();// 待付款
			List<Order> orderListSend = new ArrayList<Order>();// 待发货
			List<Order> orderListStates = new ArrayList<Order>();// 已收货
			List<Order> orderListTuihuochuli = new ArrayList<Order>();//退货处理
			List<Order> orderListTuihuo = new ArrayList<Order>();//退货
			for (Order a : orderList1) {
				int result1= a.getClothes().getCimgurl().indexOf('/');
			    if(result1 != -1){
			    	a.getClothes()
					.setCimgurl(a.getClothes().getCimgurl().substring(0, a.getClothes().getCimgurl().indexOf('/')));
			    }else{
			    }}
			for (Order c : orderList1) {
				counts++;
				if (c.getOstate() == 0 && c.getOsend() == 1) {
					countState++;// 待收货
					orderListState.add(c);
				}
				if (c.getOpayed() == 0 && c.getOorder() == 0) {
					countPayed++;// 待付款
					orderListPayed.add(c);
				}
				if (c.getOsend() == 0 && c.getOpayed() == 1) {
					countSend++;// 待发货
					orderListSend.add(c);
				}
				if (c.getOstate() == 1 && c.getOtuihuo() == 0) {
					countStates++;// 已收货
					orderListStates.add(c);
				}
				if (c.getOtuihuo() == 1|| c.getOtuihuo() == 2) {
					countTuihuochuli++;// 已货处理
					orderListTuihuochuli.add(c);
				}
				if (c.getOtuihuo() == 3) {
					countTuihuo++;// 已退货
					orderListTuihuo.add(c);
				}
			}
			
			//分页处理
			int index = 1;// 默认显示第一页
			if (request.getParameter("page") != null) {// 表示是通过请求别的页数进来
				index = Integer.valueOf(request.getParameter("page"));
			}
			int pagesize = 10;// 每页显示5条
			int count = aos.getCount();
			int pagecount = count % pagesize == 0 ? (count / pagesize) : (count / pagesize + 1);// 总页数
			// 下容错比如请求的数据是-5页（向下容错跟1比较 比1小那么默认显示第一页）
			index = index < 1 ? 1 : index;
			// 上容错比如请求的数据是999页（向上容错跟总页数比较 比总页数大默认显示最后一页）
			index = index > pagecount ? pagecount : index;
			List<Order> orderList = aos.queryByPage(index, pagesize);
			for (Order a : orderList) {
				int result1= a.getClothes().getCimgurl().indexOf('/');
			    if(result1 != -1){
			    	a.getClothes()
					.setCimgurl(a.getClothes().getCimgurl().substring(0, a.getClothes().getCimgurl().indexOf('/')));
			    }else{
			    }
			
			}
			int shangpage = index - 1;
			int xiapage = index + 1;
			
			//计数
			request.setAttribute("counts", counts);
			request.setAttribute("countTuihuo", countTuihuo);
			request.setAttribute("countTuihuochuli", countTuihuochuli);
			request.setAttribute("countStates", countStates);
			request.setAttribute("countState", countState);
			request.setAttribute("countPayed", countPayed);
			request.setAttribute("countSend", countSend);
			//查询结果集合
			request.setAttribute("orderList", orderList);
			request.setAttribute("orderListState", orderListState);
			request.setAttribute("orderListPayed", orderListPayed);
			request.setAttribute("orderListSend", orderListSend);
			request.setAttribute("orderListStates", orderListStates);
			request.setAttribute("orderListTuihuo", orderListTuihuo);
			request.setAttribute("orderListTuihuochuli", orderListTuihuochuli);
			
			request.setAttribute("shangpage", shangpage);
			request.setAttribute("xiapage", xiapage);
			request.setAttribute("pagecount", pagecount);
			request.setAttribute("index", index);
			request.getRequestDispatcher("Adminsellerorder.jsp").forward(request, response);
		}

	}
}
